package com.daka.controller;

import com.daka.entity.Member;
import com.daka.entity.MemberOrg;
import com.daka.entity.Organization;
import com.daka.enumerate.StatusCode;
import com.daka.service.MemberOrgService;
import com.daka.service.MemberService;
import com.daka.service.OrganizationService;
import com.daka.tools.MD5Util;
import com.daka.vo.request.MemberVo;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.MemberResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangx
 * CreateTime: 2016/9/18 15:15
 */

@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberOrgService memberOrgService;
    @Autowired
    private OrganizationService organizationService;


    @ResponseBody
    @RequestMapping(value = "/register", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> register(@RequestBody MemberVo vo){
        String groupName = vo.getGroupName();
        if(vo.getMobile()==null || vo.getPassword()==null || vo.getRealName()==null
          || vo.getCardType()==null || vo.getCardNO()==null || vo.getProvince()==null
          || vo.getCity()==null|| vo.getDistrict()==null){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(),StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        //判断手机号是否已注册
        Member member1 = new Member();
        member1.setBundMobile(vo.getMobile());
        List<Member> listMember = memberService.selectByStatement(member1);
        if(listMember != null && listMember.size()>0){
            return new DataResponseVo<Object>(StatusCode.USER_EXISTS.getCode(),"该手机号码注册了");
        }
        //身份证是否已经使用
        Member memberCardNo = new Member();
        memberCardNo.setCardNO(vo.getCardNO());
        List<Member> listMemberCradNo = memberService.selectByStatement(memberCardNo);
        if(listMemberCradNo != null && listMemberCradNo.size()>0){
            return new DataResponseVo<Object>(StatusCode.USER_EXISTS.getCode(),"该身份证号码注册过了");
        }
        vo.setSchool(vo.getGroupName());
        //创建志愿者
        memberService.insert(vo);
        Map<String ,Object> map = new HashMap<String, Object>();
        //判断组织是否存在
        Organization organization = new Organization();
        organization.setName(groupName);
        List<Organization> listOrg = organizationService.selectByStatement(organization);
        if(listOrg != null && listOrg.size()!=0){
            map.put("groupCode", listOrg.get(0).getGroupCode());
            //加入组织
            MemberOrg memberOrg = new MemberOrg();
            memberOrg.setOrgid(listOrg.get(0).getId());
            memberOrg.setMember(vo.getId());
            List<MemberOrg> listTemp = memberOrgService.selectByStatement(memberOrg);
            if(listTemp.size()==0){
                memberOrgService.insertPU(memberOrg);
            }
        }else{
            //创建、加入组织
            organization.setCreateUser(vo.getId());
            organization.setProvince(vo.getProvince());
            organization.setCity(vo.getCity());
            organization.setDistrict(vo.getDistrict());
            organizationService.insert(organization);
            System.out.println("organizationd的id：" + organization.getId());
            Organization org = organizationService.selectByPrimaryKey(organization.getId());
            map.put("groupCode", org.getGroupCode());
        }
        Member member2 = memberService.selectByPrimaryKey(vo.getId());
        map.put("memberID",member2.getMemberID());

        return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(),map);
    }


    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> login (@RequestBody MemberVo vo){
        if(vo.getMobile()==null || vo.getGroupName()==null || vo.getPassword()==null){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(),StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        String mobile = vo.getMobile();
        String password = vo.getPassword();
        String groupName = vo.getGroupName();

        Map<String ,Object> map = new HashMap<String, Object>();
        //判断手机号是否已注册
        Member member1 = new Member();
        member1.setBundMobile(mobile);
        List<Member> listMember = memberService.selectByStatement(member1);
        if(listMember==null || listMember.size()==0){
            return new DataResponseVo<Object>(StatusCode.USER_NOT_EXISTS.getCode(),StatusCode.USER_NOT_EXISTS.getMessage());
        }

        String temp = MD5Util.MD5(password);
        if (!listMember.get(0).getPassword().toLowerCase().equals(temp.toLowerCase())){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(),"密码错误！");
        }
        Member member2 = memberService.selectByPrimaryKey(listMember.get(0).getId());
        //判断组织是否存在
        Organization organization = new Organization();
        organization.setName(groupName);
        List<Organization> listOrg = organizationService.selectByStatement(organization);
        if(listOrg != null && listOrg.size()!=0){
            map.put("groupCode",listOrg.get(0).getGroupCode());
            //是否加入组织
            MemberOrg memberOrg = new MemberOrg();
            memberOrg.setMember(member2.getId());
            memberOrg.setOrgid(listOrg.get(0).getId());
            List<MemberOrg> listMO = memberOrgService.selectByStatement(memberOrg);
            if(listMO.size()==0){
                memberOrgService.insertPU(memberOrg);
            }
        }else{
            //创建、加入组织
            organization.setCreateUser(member2.getId());
            organization.setProvince(member2.getProvince());
            organization.setCity(member2.getCity());
            organization.setDistrict(member2.getDistrict());
            organizationService.insert(organization);
            System.out.println("organizationd的id：" + organization.getId());
            Organization org = organizationService.selectByPrimaryKey(organization.getId());
            map.put("groupCode", org.getGroupCode());
        }
        map.put("memberID",member2.getMemberID());

        return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(),map);
    }


    @ResponseBody
    @RequestMapping(value = "/queryDetail", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> queryDetail(@RequestBody MemberVo vo){
        if(vo.getMemberID()==null){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(),StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        String memberId = vo.getMemberID();
        Member member = new Member();
        member.setMemberID(memberId);
        List<Member> list = memberService.selectByStatement(member);
        if(list==null || list.size()==0){
            return new DataResponseVo<Object>(StatusCode.USER_NOT_EXISTS.getCode(),StatusCode.USER_NOT_EXISTS.getMessage());
        }
        MemberResponseVo memberResponseVo = MemberResponseVo.buildVO(list.get(0));
        return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(),memberResponseVo);
    }

}




